/*******************************************************
 * 検証専用JS
 *******************************************************/
var KENTO = KENTO || {}; //検討リスト
var RECENT = RECENT || {}; //最近見た物件リスト
var CONS = CONS || {}; //検索条件

KENTO.init = function(){	
	
	//詳細ページ
	$('.type_list_wrap .mod_table').find('a.mod_icn_star').each(function(e){		
		var heyacd = $(this).prev().val();
		
		if(inKentoList(heyacd)){	
			$(this).removeClass('mod_icn_star');
			$(this).addClass('mod_icn_star_active');			
		}
		else{
			$(this).addClass('mod_icn_star');
			$(this).removeClass('mod_icn_star_active');
		}
	});
	
	//詳細ページ検討リスト
	$('.btn_add a.mod_btn_s').each(function(e){
		var heyacd = $(this).prev().val();		
		if(inKentoList(heyacd)){	
			$(this).addClass('mod_btn_active');			
		}
	});
	
	//一覧ページ
	$('#contentMainColumn .mod_box_section_bdt .mod_table').find('a.mod_icn_star').each(function(e){
		var heyacd = $(this).prev().val();		
		if(inKentoList(heyacd)){	
			$(this).removeClass('mod_icn_star');
			$(this).addClass('mod_icn_star_active');			
		}
		else{
			$(this).addClass('mod_icn_star');
			$(this).removeClass('mod_icn_star_active');
		}
	});
	
	
	//一覧ページ（Picビュー）
	$('#contentMainColumn .wrapper_picture').find('a.mod_icn_star').each(function(e){
		var heyacd = $(this).prev().val();		
		if(inKentoList(heyacd)){	
			$(this).removeClass('mod_icn_star');
			$(this).addClass('mod_icn_star_active');			
		}
		else{
			$(this).addClass('mod_icn_star');
			$(this).removeClass('mod_icn_star_active');
		}
	});
	
	
	//問い合わせ
	$('.mod_table_col').find('a.mod_icn_star').each(function(e){		
		var heyacd = $(this).prev().val();
		
		if(inKentoList(heyacd)){	
			$(this).removeClass('mod_icn_star');
			$(this).addClass('mod_icn_star_active');			
		}
		else{
			$(this).addClass('mod_icn_star');
			$(this).removeClass('mod_icn_star_active');
		}
	});
	
	//スマホ詳細ページ
	var $target = $('.mod_btn_add_studylist');
	$('.mod_btn_add_studylist a').each(function(e){
		var heyacd = $(this).attr('id');		
		if(inKentoList(heyacd)){
			$target.removeClass('mod_btn_add_studylist');
			$target.addClass('mod_btn_add_studylist_active');
			$target.find('a').addClass('disabled');
			$target.find('a').unbind('click');
		}		
	});
	
	//スマホ閲覧済
	$('.txt_visited').each(function(){
		var heyaCd = $(this).attr('id');		
		if(inRecentList(heyaCd)){
			$(this).show();
		}
		else{
			$(this).hide();
		}
			
	});
	//スマホ閲覧済
	
	//検討リスクに物件追加されるかどうかのチェック
	function inKentoList(heyacd){	
		var kenVals = $.cookie('kentoList');
		if(kenVals == undefined || kenVals == '') return false;
		var kentoList = kenVals.split(',');
		if(kentoList.length == 0) return false;
		
		if($.inArray(heyacd, kentoList) != -1) return true; //追加された。
		return false;
	}
	
	//最近閲覧物件追加されるかどうかのチェック
	function inRecentList(heyacd){	
		var recentVals = $.cookie('recentList');
		if(recentVals == undefined || recentVals == '') return false;
		var recentList = recentVals.split(',');
		if(recentList.length == 0) return false;
		
		if($.inArray(heyacd, recentList) != -1) return true; //追加された。
		return false;
	}
		
	//検討件数	
	$('.btn_study_list').find('.number').html(KENTO.count());	
	
	//SP検討リスク
	$('.btn_study_list').find('.total').find('span').html(KENTO.count());
};

//検討件数カウント
KENTO.count = function(){
	var count = 0;
	var kentoVals = $.cookie('kentoList');
	if(kentoVals != undefined && kentoVals != ''){
		count = kentoVals.split(',').length;
	}
	return count;
}

//検討リスク追加
KENTO.addKentoList = function(heyaCd){	
	var kentoVals = $.cookie('kentoList');
	var kentoList = [];
	if(kentoVals != undefined && kentoVals != ''){
		var vals = kentoVals.split(',');
		for(var i = vals.length - 1; i >= 0; i--){
			kentoList.push(vals[i]);
		}
	}
	
	//検討リスクにまだ追加されない→追加
	if($.inArray(heyaCd, kentoList) == -1){
		
		//100まで追加できる
		if(kentoList.length >= 100){
			kentoList.splice(99,1);
		}
		kentoList.push(heyaCd);	
		$.cookie('kentoList',kentoList.join(','), {path: '/' ,expires: 10 });	
	}
	//件数表示
	$('.btn_study_list').find('.number').html(kentoList.length);
	//SP検討リスク
	$('.btn_study_list').find('.total').find('span').html(KENTO.count());
};

//検討リストから削除
KENTO.removeKentoList = function(heyaCd){
	var kentoVals = $.cookie('kentoList');
	var kentoList = [];
	if(kentoVals == undefined || kentoVals == '') return;
	
	var vals = kentoVals.split(',');
	for(var i = vals.length - 1; i >= 0; i--){
		if(vals[i] != heyaCd) kentoList.push(vals[i]);
	}	
	
	$.cookie('kentoList',kentoList.join(','), {path: '/' ,expires: 10 });	
	
	//件数表示
	$('.btn_study_list').find('.number').html(kentoList.length);
}

//最近見た物件リスト
RECENT.addToList = function(heyaCd){
	var recent = $.cookie('recentList');
	if(recent == undefined || recent == ''){
		$.cookie('recentList',heyaCd, {path: '/' ,expires: 21 });
		return;
	}
	
	//保存された→トップにアップ
	if(heyaCd == undefined || heyaCd == '' || heyaCd == null) return;
	if(recent.indexOf(heyaCd) >= 0){
		recent = recent.replace(',' + heyaCd, '').replace(heyaCd + ',', '').replace(heyaCd, '');
		if(recent == '') recent = heyaCd;
		else recent = heyaCd + ',' + recent;
	}
	else {
		recent = heyaCd + ',' + recent;
	}
	
	//20件しか保存しない
	var newList = [];
	var recentList = recent.split(',');
	var size = 20;
	if(size > recentList.length) size = recentList.length;
	for(var i = 0; i < size; i++){
		newList.push(recentList[i]);
	}

	//クッキー保存	
	$.cookie('recentList',newList.join(','), {path: '/' ,expires: 21 });
	
}
KENTO.removeToiawaseBuken = function(){
	
	//一つ部屋の問い合わせ
	$('input[name=bukken_code]').each(function(){
		KENTO.removeKentoList($(this).val());
	});
	
	//一つ部屋の問い合わせ
	$('input[name^=BukkenCd]').each(function(){
		KENTO.removeKentoList($(this).val());
	});
}

//最近見た物件リスト

////検索条件問い合わせ
//CONS.addInquiryConditon = function(conditions, itemsName){
//	var storage = localStorage;
//	storage.setItem('inquiryCons', conditions.join('|'));
//	storage.setItem('inquiryConsName', JSON.stringify(itemsName));
//	console.dir(storage.getItem('inquiryCons'));
//}

//検索条件保存
CONS.addCondition = function(conditions, itemsName){
	var cons1 = $.cookie('condition1');
	var cons2 = $.cookie('condition2');
	var cons3 = $.cookie('condition3');
	
	var storage = localStorage;
	var cons1Name = storage.getItem('condition1Text');
	var cons2Name = storage.getItem('condition2Text');
	var cons3Name = storage.getItem('condition3Text');

	//条件ない→1個目だけ保存
	if(cons1 == undefined || cons1 == ''){
		$.cookie('condition1', conditions.join('|'), {path: '/' ,expires: 7 });		
		storage.setItem('condition1Text', JSON.stringify(itemsName));
	}
	//条件1個あり→2個目まで保存
	else if(cons2 == undefined || cons2 == ''){
		$.cookie('condition1', conditions.join('|'), {path: '/' ,expires: 7 });
		$.cookie('condition2', cons1, {path: '/' ,expires: 7 });
		
		storage.setItem('condition1Text', JSON.stringify(itemsName));
		storage.setItem('condition2Text', cons1Name);
	}
	else {　//条件2個あり→3個目まで保存
		$.cookie('condition1', conditions.join('|'), {path: '/' ,expires: 7 });
		$.cookie('condition2', cons1, {path: '/' ,expires: 7 });
		$.cookie('condition3', cons2, {path: '/' ,expires: 7 });
		
		storage.setItem('condition1Text', JSON.stringify(itemsName));
		storage.setItem('condition2Text', cons1Name);
		storage.setItem('condition3Text', cons2Name);
	}
	//console.dir(conditions);
}
//検索条件保存

CONS.addTempCondition = function(conditions, itemsName){
	localStorage.setItem('TEMPCONS', conditions);
	localStorage.setItem('TEMPCONSNAME', itemsName);
;}

//条件削除
CONS.removeCondition = function(index){
	var storage = localStorage;
	var cons1 = $.cookie('condition1');
	var cons2 = $.cookie('condition2');
	var cons3 = $.cookie('condition3');
	
	var cons1Name = storage.getItem('condition1Text');
	var cons2Name = storage.getItem('condition2Text');
	var cons3Name = storage.getItem('condition3Text');
	
	var count = 1;
	if(cons3 != undefined && cons3 != null) count++;
	if(cons2 != undefined && cons2 != null) count++;
	
	if(index == '1'){
		$.cookie('condition2', cons3, {path: '/' ,expires: 7 });
		$.cookie('condition1', cons2, {path: '/' ,expires: 7 });
		storage.setItem('condition2Text', cons3Name);
		storage.setItem('condition1Text', cons2Name);
	}
	else if(index == '2'){
		$.cookie('condition2', cons3, {path: '/' ,expires: 7 });
		storage.setItem('condition2Text', cons3Name);		
	}
	$.removeCookie('condition' + String(count),{path: '/'});
	storage.removeItem('condition2Text' +  + String(count) + 'Text',{path: '/'});
	window.location.reload();
}

//条件すべて削除
CONS.removeConditionAll = function(index){
	$.removeCookie('condition1',{path: '/'});
	$.removeCookie('condition2',{path: '/'});
	$.removeCookie('condition3',{path: '/'});
	localStorage.removeItem('condition1Text');
	localStorage.removeItem('condition2Text');
	localStorage.removeItem('condition3Text');
	window.location.reload();
}

//-------------------------------------
//クッキーをクリア
//-------------------------------------
CONS.clearCookie = function(){
	$.removeCookie('kentoList',{path: '/'});
	$.removeCookie('condition1',{path: '/'});
	$.removeCookie('condition2',{path: '/'});
	$.removeCookie('condition3',{path: '/'});
	localStorage.removeItem('condition1Text');
	localStorage.removeItem('condition2Text');
	localStorage.removeItem('condition3Text');
	$.removeCookie('SAVE_CONS',{path: '/'});
	$.removeCookie('recentList',{path: '/'});
	localStorage.removeItem('TEMPCONS');
	localStorage.removeItem('TEMPCONSNAME');
	$.removeCookie('simpleEki',{path: '/'}); //簡単検索
}

/**************************************************
 * 簡単検索駅を保存
 *************************************************/
CONS.saveSimpleEki = function(val){
	$.cookie('simpleEki', val, {path: '/' ,expires: 7 });
}
/**************************************************
 * 簡単検索駅を保存
 *************************************************/

/**************************************************
 * チェックリンク
 * ************************************************
 */
function submit(action, method, values) {
   var form = $('<form/>', {
        action: action,
        method: method
    });
    $.each(values, function() {
        form.append($('<input/>', {
            type: 'hidden',
            name: this.name,
            value: this.value
        }));    
    });
    form.appendTo('body').submit();
}

//人気の駅から探す
$('a[name=toplink2]').on('click', function(event){
	event.preventDefault();
	
	//検索クッキーあります。
	if(APMN.hadSearch()){
		
		items = APMN.parseConditionCookie();
		items.push({name:'search_type',value:'line'});
		items.push({name:'target_0', value:$(this).attr('id')});
		submit('/rstobi/rstobi','POST',items);
		//console.dir(items);
	}
	else{ //希望条件に飛ばす
		var items = [];
		items.push({name:'search_type',value:'line'});
		items.push({name:'target_0', value:$(this).attr('id')});
		submit('/area/hope/','POST',items);		
		
	}
});

//人気のエリアから探す
$('a[name=toplink1]').on('click', function(event){
	event.preventDefault();
	
	//検索クッキーあります。
	if(APMN.hadSearch()){
		
		items = APMN.parseConditionCookie();
		items.push({name:'search_type',value:'area'});
		
		//市区町村分析
		var vals = $(this).attr('id').split('-');
		var rets = vals[1].split('.');
		for(var i = 0 ; i < rets.length ; i++){
			items.push({name:'target_' + String(i), value:vals[0] + '-' + vals[0] + rets[i]});			
		}
		//console.dir(items);
		submit('/rstobi/rstobi','POST',items);
	}
	else{ //希望条件に飛ばす
		var items = [];
		items.push({name:'search_type',value:'area'});
		var vals = $(this).attr('id').split('-');
		var rets = vals[1].split('.');
		for(var i = 0 ; i < rets.length ; i++){
			items.push({name:'target_' + String(i), value:vals[0] + '-' + vals[0] + rets[i]});			
		}
		
		submit('/area/hope/','POST',items);		
		
	}
});


/**************************************************
 * ************************************************
 */

KENTO.init();
